package org.jeecg.modules.analysis.graph.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.analysis.graph.dto.GraphHistorySaveDTO;
import org.jeecg.modules.analysis.graph.entity.GraphHistory;
import org.jeecg.modules.analysis.graph.service.IGraphHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 图谱历史记录 Controller
 * @Author: dingmengdi
 * @Date: 2025-10-13
 * @Version: V1.0
 */
@Tag(name = "图谱历史记录")
@RestController
@RequestMapping("/analysis/graph/history")
@Slf4j
public class GraphHistoryController extends JeecgController<GraphHistory, IGraphHistoryService> {

    @Autowired
    private IGraphHistoryService graphHistoryService;

    /**
     * 保存访问记录（用户信息由后端自动获取）
     */
    @AutoLog(value = "图谱历史-保存访问记录")
    @Operation(summary = "图谱历史-保存访问记录")
    @PostMapping(value = "/save")
    public Result<String> saveHistory(@RequestBody GraphHistorySaveDTO dto) {
        try {
            graphHistoryService.saveOrUpdateHistory(dto.getPatentId(), dto.getPatentName(), dto.getRelationType());
            return Result.OK("保存成功");
        } catch (Exception e) {
            log.error("保存访问记录失败", e);
            return Result.error("保存失败: " + e.getMessage());
        }
    }

    /**
     * 获取当前用户最后访问的图谱记录（用户信息由后端自动获取）
     */
    @AutoLog(value = "图谱历史-获取最后访问记录")
    @Operation(summary = "图谱历史-获取最后访问记录")
    @GetMapping(value = "/getLastVisit")
    public Result<GraphHistory> getLastVisit() {
        try {
            GraphHistory lastVisit = graphHistoryService.getLastVisit();
            if (lastVisit != null) {
                return Result.OK(lastVisit);
            } else {
                return Result.error("未找到访问记录");
            }
        } catch (Exception e) {
            log.error("获取最后访问记录失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}

